<?php
class User_login extends CI_Controller
{
    public function __construct(){
        parent::__construct();
         $this->load->helper('form');
          $this->load->helper('validation_helper');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('user_model');
    }
    
    public function login(){
         $this->form_validation->set_rules(
            array(
                array('field' => 'user_email', 'label' => 'Email', 'rules' => 'trim|required|valid_email|callback_email_check'),
                array('field' => 'user_password', 'label' => 'Password', 'rules' => 'trim|required|callback_password_check'), 
                ));
         if ($this->form_validation->run()==FALSE)
        {
                
              	$data['message'] = (validation_errors()) ? validation_errors() :'';
                
             $this->template->write_view('content', 'user_login', $data, $overwrite = FALSE);
             redirect();
        }         
       /* if ($this->form_validation->run($this))
        {
          $cond['user_email']=$this->input->post('user_email');
          $cond['user_password']=MD5($this->input->post('user_password'));
          $r=$this->user_model->get_users($cond)->num_rows();
          echo "ID".$r; 
          if($r>0){
            $this->session->set_userdata('user_id',$r);
            redirect('shiporder_main/index/login_success');
          }            
          else
            redirect('shiporder_main/index/');
               
        }else{
            
            $this->template->write_view('content', 'user_login', '', $overwrite = FALSE);
            redirect();
            
        } */
    }
     

    function password_check($password)
    {
      
        $con['user_password']=MD5($this->input->post('user_password'));
        if (!$this->input->post('user_email')
             && $this->input->post('user_password') 
             &&  $this->user_model->get_users( $con)
             )
        {
            $this->form_validation->set_message('password_check', 'Password is incorrect.');
            return FALSE;
        }
        return TRUE;
    }
    function logout(){
        $this->session->unset_userdata('user_id');
        
         $this->template->write_view('content', 'user_login', '', $overwrite = FALSE);
         redirect('shiporder_main/login');
            //$this->template->render();
    }
} 
?>